$.extend($.fn.datagrid.methods, {
	editCell: function(jq,param){
		return jq.each(function(){
			var opts = $(this).datagrid('options');
			var fields = $(this).datagrid('getColumnFields',true).concat($(this).datagrid('getColumnFields'));
			for(var i=0; i<fields.length; i++){
				var col = $(this).datagrid('getColumnOption', fields[i]);
				col.editor1 = col.editor;
				if (fields[i] != param.field){
					col.editor = null;
				}
			}
			$(this).datagrid('beginEdit', param.index);
			for(var i=0; i<fields.length; i++){
				var col = $(this).datagrid('getColumnOption', fields[i]);
				col.editor = col.editor1;
			}
		});
	}
});
var editIndex = undefined;
function endEditing(){
	if (editIndex == undefined){return true}
	if ($('#dg').datagrid('validateRow', editIndex)){
		$('#dg').datagrid('endEdit', editIndex);
		editIndex = undefined;
		return true;
	} else {
		return false;
	}
}
function onClickCell(index, field){
	if (endEditing()){
		$('#dg').datagrid('selectRow', index)
				.datagrid('editCell', {index:index,field:field});
		editIndex = index;
	}
}
function onClickRow(index){
	if (editIndex != index){
		if (endEditing()){
			$('#dg').datagrid('selectRow', index)
					.datagrid('beginEdit', index);
			editIndex = index;
		} else {
			$('#dg').datagrid('selectRow', editIndex);
		}
	}
}
function removeit(){
	var rows = $('#dg').datagrid('getChecked');
	if(rows){
		var data=JSON.stringify(rows)
		var success=function(){
			$.messager.alert("提示", "提交成功！");
			$.each(rows,function(n,value){
				var idx=$('#dg').datagrid('getRowIndex',value);
				$('#dg').datagrid('cancelEdit', idx)
				        .datagrid('deleteRow', idx);
			});
		}
		var err=function(error){
			$.messager.alert("提示", error);
		};
		control('deleteNav', data,success,err);
		
	}
	
	
}
function accept(){
	if (endEditing()){
		var datas,crl='saveNav';
		var inserted = $('#dg').datagrid('getChanges', "inserted");
		if(inserted.length)datas=inserted;
		else{
			datas=$('#dg').datagrid('getChanges');
			crl='updateNav';
		}
		for(var i=0;i<datas.length;i++){
			var dt=datas[i];
			if(dt.check!=undefined)delete dt.check;
		}
		var data=JSON.stringify(datas);
		var success=function(){
			$.messager.alert("提示", "提交成功！");
			$('#dg').datagrid('acceptChanges');
		}
		var err=function(error){
			$.messager.alert("提示", error);
		};
		control(crl,data,success,err);
		
	}
}
function reject(){
	$('#dg').datagrid('rejectChanges');
	editIndex = undefined;
}
function getChanges(){
	var rows = $('#dg').datagrid('getChanges');
	alert(rows.length+' rows are changed!');
}
function append(){
	if (endEditing()){
		$('#dg').datagrid('appendRow',{check:'1',id:uuid(),code:'',name:'',fun_url:'',sort_no:'',pid:'',type:'',remark:''});
		editIndex = $('#dg').datagrid('getRows').length-1;
		$('#dg').datagrid('selectRow', editIndex);
	}
}
function uuid() {
	var s = [];
	var hexDigits = "0123456789abcdefjhigklmnopqrstuvwxyz";
	for (var i = 0; i < 36; i++) {
	s[i] = hexDigits.substr(Math.floor(Math.random() * 0x10), 1);
	}
	s[14] = "4";
	s[19] = hexDigits.substr((s[19] & 0x3) | 0x8, 1); 
	s[8] = s[13] = s[18] = s[23] = "-";
	var uuid = s.join("");
	uuid=uuid.replace(/-/g,"");
	uuid=uuid.toUpperCase();
	return uuid;
}
function control(url, data,success,err){
	$.ajax({ url: url,
	     type:"post",
	     contentType : 'application/json', 
	     data:data,
	     success: function(){
	    	 if(success)success();
         },
         error : function(error){
        	 if(err)err(error);
         }
     });
}
function pagerFilter(data){
	if (typeof data.length == 'number' && typeof data.splice == 'function'){	// is array
		data = {
			total: data.length,
			rows: data
		}
	}
	var dg = $(this);
	var opts = dg.datagrid('options');
	var pager = dg.datagrid('getPager');
	pager.pagination({
		beforePageText:'第',
		afterPageText:'页, 共{pages}页',
		displayMsg:'当前显示从第{from}条到{to}条 共{total}条记录',
		onSelectPage:function(pageNum, pageSize){
			opts.pageNumber = pageNum;
			opts.pageSize = pageSize;
			pager.pagination('refresh',{
				pageNumber:pageNum,
				pageSize:pageSize
			});
			dg.datagrid('loadData',data);
		}
	});
	if (!data.originalRows){
		data.originalRows = (data.rows);
	}
	var start = (opts.pageNumber-1)*parseInt(opts.pageSize);
	var end = start + parseInt(opts.pageSize);
	data.rows = (data.originalRows.slice(start, end));
	return data;
}

$(function(){
	$('#dg').datagrid({loadFilter:pagerFilter});
});